<?php

namespace app\index\controller;

use think\Controller;

class Base extends Controller
{
    //初始化
    public function initialize()
    {
        $cates = model('./cate')->select();
        $systems = model('system')->find();
        $articleTop = model('Article')->where('is_top',1)->select();
        $nickname = "";
        if (session('?index.id')) {
            $nickname = session('index.nickname');
        }
        $this->assign([
            "cates" => $cates,
            "systems" => $systems,
            "nickname" => $nickname,
            "articleTop" => $articleTop
        ]);
        return view();
    }

    //搜索
    public function search()
    {
        if (request()->isAjax()) {
            $data = [
                "title" => input('val')
            ];
            $result = model('Article')->withSearch(['title'], $data)->select();
            $this->assign("list", $result);
            return json($result);
        }
    }

    //退出登录
    public function logout(){
        session(null);
        cookie("id", null);
        if (!session('?index')){
            $this->success('退出成功', 'index/index/index');
        }else{
            $this->error('退出失败');
        }
    }
}
